package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-perm/cache"
	"logi-perm/models"
	"strconv"
	"time"
)

func UserDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var user models.User
	redis_key := "user_detail_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Offset(offset).Limit(size).Debug().Find(&user)
		json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, cacheData, time.Hour*2)
	}
	var total int64
	json.Unmarshal([]byte(cacheData), &user).Error()
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"total": total,
	})
}
